﻿@model int
@using Nop.Core;
@using Nop.Web.Framework.UI;
@{
    //register CSS and JS
    Html.AddCssFileParts("~/Scripts/fineuploader/fineuploader-4.2.2.min.css");
    Html.AddScriptParts("~/Scripts/fineuploader/jquery.fineuploader-4.2.2.min.js");
    
    //other variables
    var randomNumber = CommonHelper.GenerateRandomInteger();
    var clientId = "download" + randomNumber;
    var downloadService = EngineContext.Current.Resolve<Nop.Services.Media.IDownloadService>();
    var download = downloadService.GetDownloadById(Model);

    var disableUrl = Convert.ToBoolean(ViewData["nop.DownloadEditor.DisableUrl"] ?? false);
}

<script type="text/javascript">
    $(document).ready(function() {
        $('#cbUseDownloadURL@(randomNumber)').click(toggleDownloadRecordType@(randomNumber));

        $('#saveDownloadUrl@(randomNumber)').click(function() {
            var downloadUrl = $("#downloadurl@(randomNumber)").val();
            $('#saveDownloadUrl@(randomNumber)').attr('disabled', true);
            $.ajax({
                cache: false,
                type: "POST",
                url: "@(Url.Action("SaveDownloadUrl", "Download"))",
                data: { "downloadUrl": downloadUrl },
                success: function(data) {
                    $('#pnlDownloadURLResult@(randomNumber)').fadeIn("slow").delay(1000).fadeOut("slow");
                    $("#@(clientId + "value") input").val(data.downloadId);
                    $('#saveDownloadUrl@(randomNumber)').attr('disabled', false);

                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert('Failed to save download object.');
                    $('#saveDownloadUrl@(randomNumber)').attr('disabled', false);
                }
            });
        });

        toggleDownloadRecordType@(randomNumber)();
    });

    function toggleDownloadRecordType@(randomNumber)() {
        if ($('#cbUseDownloadURL@(randomNumber)').is(':checked')) {
            $('#pnlDownloadURL@(randomNumber)').show();
            $('#pnlDownloadFile@(randomNumber)').hide();
        } else {
            $('#pnlDownloadURL@(randomNumber)').hide();
            $('#pnlDownloadFile@(randomNumber)').show();
        }
    }

</script>
<div id="@(clientId + "value")">
    @Html.HiddenFor(x => x)
</div>
<table class="adminContent" border="1" style="width:500px">
    @if (!disableUrl)
    {
        <tr>
            <td class="adminTitle">
                @T("Admin.Download.UseDownloadURL"):
            </td>
            <td class="adminData">
                <input type="checkbox" name="cbUseDownloadURL@(randomNumber)" id="cbUseDownloadURL@(randomNumber)"
                       @if (download != null && download.UseDownloadUrl) { <text> checked="checked" </text>   } />
            </td>
        </tr>
    }
    <tr id="pnlDownloadURL@(randomNumber)">
        <td class="adminTitle">
            @T("Admin.Download.DownloadURL"):
        </td>
        <td class="adminData">
            <input type="text" name="downloadurl@(randomNumber)" id="downloadurl@(randomNumber)"
                @if (download != null && !String.IsNullOrEmpty(download.DownloadUrl))
                {
                    <text>value="@(download.DownloadUrl)"</text>
                }
                />
            <input type="button" id="saveDownloadUrl@(randomNumber)" class="k-button" value="@T("Admin.Download.SaveDownloadURL")" />
            <div id="pnlDownloadURLResult@(randomNumber)" style="display:none;">@T("Admin.Download.DownloadURLSaved")</div>
        </td>
    </tr>
    <tr id="pnlDownloadFile@(randomNumber)">
        <td class="adminTitle">
            @T("Admin.Download.UploadFile"):
        </td>
        <td class="adminData">
            <div>
                @*fine uploader container*@
                <div id="@clientId">
                    <noscript>
                        <p>
                            Please enable JavaScript to use file uploader.</p>
                        <!-- or put a simple form for upload here -->
                    </noscript>
                </div>                    
                @*fine uploader template (keep it synchronized to \Content\fineuploader\templates\default.html)*@
                <script type="text/template" id="@(clientId)-qq-template">
                        <div class="qq-uploader-selector qq-uploader">
                            <div class="qq-upload-drop-area-selector qq-upload-drop-area" qq-hide-dropzone>
                                <span>@T("Common.FileUploader.DropFiles")</span>
                            </div>
                            <div class="qq-upload-button-selector qq-upload-button">
                                <div>@T("Common.FileUploader.Upload")</div>
                            </div>
                            <span class="qq-drop-processing-selector qq-drop-processing">
                                <span>@T("Common.FileUploader.Processing")</span>
                                <span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
                            </span>
                            <ul class="qq-upload-list-selector qq-upload-list">
                                <li>
                                    <div class="qq-progress-bar-container-selector">
                                        <div class="qq-progress-bar-selector qq-progress-bar"></div>
                                    </div>
                                    <span class="qq-upload-spinner-selector qq-upload-spinner"></span>
                                    <span class="qq-edit-filename-icon-selector qq-edit-filename-icon"></span>
                                    <span class="qq-upload-file-selector qq-upload-file"></span>
                                    <input class="qq-edit-filename-selector qq-edit-filename" tabindex="0" type="text">
                                    <span class="qq-upload-size-selector qq-upload-size"></span>
                                    <a class="qq-upload-cancel-selector qq-upload-cancel" href="#">@T("Common.FileUploader.Cancel")</a>
                                    <a class="qq-upload-retry-selector qq-upload-retry" href="#">@T("Common.FileUploader.Retry")</a>
                                    <a class="qq-upload-delete-selector qq-upload-delete" href="#">@T("Common.FileUploader.Delete")</a>
                                    <span class="qq-upload-status-text-selector qq-upload-status-text"></span>
                                </li>
                            </ul>
                        </div>
                    </script>

                <script type="text/javascript">

                    $(document).ready(function () {
                        $("#@clientId").fineUploader({
                            request: {
                                endpoint: '@(Url.Content("~/Admin/Download/AsyncUpload"))'
                            },
                            template: "@(clientId)-qq-template",
                            multiple: false
                        }).on("complete", function (event, id, name, responseJSON, xhr) {
                            $("#@(clientId + "downloadurl")").html("<a href='" + responseJSON.downloadUrl + "'>@T("Admin.Download.DownloadUploadedFile")</a>");
                            $("#@(clientId + "value") input").val(responseJSON.downloadId);
                            $("#@(clientId + "remove")").show();
                        });

        
                        $("#@(clientId + "remove")").click(function(e){
                            $("#@(clientId + "downloadurl")").html("");
                            $("#@(clientId + "value") input").val(0);
                            $(this).hide();
                        });
                    });

                </script>

            </div>
            <div id="@(clientId + "downloadurl")">
                @if (download != null)
                {
                    <a href="@(Url.Action("DownloadFile", "Download", new { downloadGuid = download.DownloadGuid }))">@T("Admin.Download.DownloadUploadedFile")</a>
                }
            </div>
            <div>
                @if (download != null)
                {
                    <span id="@(clientId + "remove")" class="k-button">@T("Admin.Download.RemoveDownload")</span>
                }
                else
                {
                    <span id="@(clientId + "remove")" class="k-button" style="display:none;">@T("Admin.Download.RemoveDownload")</span>
                }
            </div>
        </td>
    </tr>
</table>


